package com.hskn.hss.module.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.module.kfkcarlocation.entity.KfkCarLocation;
import com.hskn.hss.module.kfkcarlocation.mapper.KfkCarLocationMapper;
import com.hskn.hss.module.kfkcarlocation.service.IKfkCarLocationService;
import com.hskn.hss.module.statistics.vo.KfkCarLocationListRes;
import com.hskn.hss.module.statistics.vo.TlOilcostCfgRes;
import com.hskn.hss.module.statistics.vo.TlPersonListRes;
import com.hskn.hss.module.tloilcostcfg.mapper.TlOilcostCfgMapper;
import com.hskn.hss.module.tloilcostcfg.service.ITlOilcostCfgService;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import com.hskn.hss.utils.excel.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆燃油报表
 *
 * @author hskn
 * @since 2021-05-25
 */
@Slf4j
@RestController
@RequestMapping("/hss/vehiclefuelreport")
public class VehicleFuelReportController {
    @Autowired
    private IKfkCarLocationService kfkCarLocationService;

    @Autowired
    private KfkCarLocationMapper kfkCarLocationMapper;

    @Autowired
    private ITlOilcostCfgService tlOilcostCfgService;

    @Autowired
    private TlOilcostCfgMapper tlOilcostCfgMapper;

    /**
     * 单车油量曲线-单车油量变化-图表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getDcylbhChart", method = RequestMethod.POST)
    public AjaxResult<Object> getDcylbhChart(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));

            QueryWrapper<KfkCarLocation> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("oilMass,speed,timestamp");
            if (params != null) {
                if (StringUtils.isNotEmpty(params.getString("carnum")) && params.getString("carnum") != null) {
                    queryWrapper.lambda().eq(KfkCarLocation::getCarnum, params.getString("carnum"));
                }
                String startDate = params.getString("startDate");
                String endDate = params.getString("endDate");
                if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
                    queryWrapper.lambda().between(KfkCarLocation::getDeviceTime, startDate, endDate);
                }
            }
            List<KfkCarLocation> json = kfkCarLocationService.list(queryWrapper);

            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 单车油量曲线-全部数据-列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getDcylqxQbsjList", method = RequestMethod.POST)
    public AjaxResult<Object> getDcylqxQbsjList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = kfkCarLocationService.getDcylqxQbsjList(params);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 单车油量曲线-全部数据-导出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getDcylqxQbsjExport", method = RequestMethod.POST)
    public AjaxResult<Object> getDcylqxQbsjExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            Map map = new HashMap();
            map.put("carNum", params.getString("carNum"));
            map.put("startDate", params.getString("startDate"));
            map.put("endDate", params.getString("endDate"));
            List<KfkCarLocationListRes> exportList = kfkCarLocationMapper.getDcylqxQbsjList(map);
            ExcelUtil.exportExcel(exportList, "单车油量曲线全部数据.xlsx", "全部数据", "sheet1", TlPersonListRes.class);
            json.put("exportList", exportList);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 单车油量曲线-加油数据-列表//无数据
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getDcylqxJysjList", method = RequestMethod.POST)
    public AjaxResult<Object> getDcylqxJysjList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = tlOilcostCfgService.getDcylqxJysjList(params);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 单车油量曲线-加油数据-导出//无数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getDcylqxJysjExport", method = RequestMethod.POST)
    public AjaxResult<Object> getDcylqxJysjExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            Map map = new HashMap();
            map.put("carNumid", params.getString("carNumid"));
            map.put("startDate", params.getString("startDate"));
            map.put("endDate", params.getString("endDate"));
            List<TlOilcostCfgRes> exportList = tlOilcostCfgMapper.getDcylqxJysjList(map);
            ExcelUtil.exportExcel(exportList, "单车油量曲线加油数据.xlsx", "加油数据", "sheet1", TlPersonListRes.class);
            json.put("exportList", exportList);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 单车油量曲线-漏油数据-列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getDcylqxLysjList", method = RequestMethod.POST)
    public AjaxResult<Object> getDcylqxLysjList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = tlOilcostCfgService.getDcylqxJysjList(params);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 单车油量曲线-漏油数据-导出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getDcylqxLysjExport", method = RequestMethod.POST)
    public AjaxResult<Object> getDcylqxLysjExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            Map map = new HashMap();
            map.put("carNumid", params.getString("carNumid"));
            map.put("startDate", params.getString("startDate"));
            map.put("endDate", params.getString("endDate"));
            List<TlOilcostCfgRes> exportList = tlOilcostCfgMapper.getDcylqxJysjList(map);
            ExcelUtil.exportExcel(exportList, "单车油量曲线漏油数据.xlsx", "加油数据", "sheet1", TlPersonListRes.class);
            json.put("exportList", exportList);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 耗油报表-按车汇总-图表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getHybbAchzChart", method = RequestMethod.POST)
    public AjaxResult<Object> getHybbAchzChart(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = kfkCarLocationService.getHybbAchzChart(params);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 耗油报表-时间走势-图表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getHybbSjzsChart", method = RequestMethod.POST)
    public AjaxResult<Object> getHybbSjzsChart(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = kfkCarLocationService.getHybbSjzsChart(params);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 耗油报表-油耗统计-列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getHybbYhtjList", method = RequestMethod.POST)
    public AjaxResult<Object> getHybbYhtjList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = kfkCarLocationService.getHybbYhtjList(params);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 耗油报表-油耗统计-导出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getHybbYhtjExport", method = RequestMethod.POST)
    public AjaxResult<Object> getHybbYhtjExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            Map map = new HashMap();
            map.put("carNumid", params.getString("carNumid"));
            map.put("startDate", params.getString("startDate"));
            map.put("endDate", params.getString("endDate"));
            List<KfkCarLocationListRes> exportList = kfkCarLocationMapper.getHybbYhtjExport(map);
            ExcelUtil.exportExcel(exportList, "单车油量曲线漏油数据.xlsx", "加油数据", "sheet1", TlPersonListRes.class);
            json.put("exportList", exportList);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 耗油报表-每日统计-列表
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getHybbMrtjList", method = RequestMethod.POST)
    public AjaxResult<Object> getHybbMrtjList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = kfkCarLocationService.getHybbMrtjList(params);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 耗油报表-每日统计-导出
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getHybbMrtjExport", method = RequestMethod.POST)
    public AjaxResult<Object> getHybbMrtjExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            Map map = new HashMap();
            map.put("carNumid", params.getString("carNumid"));
            map.put("startDate", params.getString("startDate"));
            map.put("endDate", params.getString("endDate"));
            List<KfkCarLocationListRes> exportList = kfkCarLocationMapper.getHybbMrtjExport(map);
            ExcelUtil.exportExcel(exportList, "单车油量曲线漏油数据.xlsx", "加油数据", "sheet1", TlPersonListRes.class);
            json.put("exportList", exportList);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }
}
